Pixel Format
#Core_Video
kCVPixelFormatType
概要
iOS や macOS のカメラから AVCaptureVideoDataOutput 経由でフレームデータを受け取る場合や、画像/映像の raw データを CVPixelBuffer で扱う場合に、Pixel Format の考慮が必要になる場合がある。Pixel Format は、画像/映像フレームデータのフォーマットであり、各ピクセルデータがとることのできる値の幅や、メモリ上に展開されたときの構成などが定められている。
Pixel Format の一覧は下記にある。
Pixel Format Identifiers - Core Video | Apple Developer Document
具体的な値も含めたリストをみたければ Microsoft の doc の方が良い。
https://docs.microsoft.com/en-us/dotnet/api/corevideo.cvpixelformattype?view=xamarin-ios-sdk-12
指定の例
AVCaptureVideoDataOutput
カメラの映像を受け取る場合、以下のように設定できる。利用可能な Pixel Format は availableVideoPixelFormatTypes で確認できる。デフォルトでは YCbCr 形式が利用されているが、ここで BGRA 形式などを指定するとその形式でフレームデータを受け取ることができて便利。
code:swift
let output = AVCaptureVideoDataOutput()
// 利用可能なPixelFormatは、avialableVideoPixelFormatTypes() で確認できる
// print(output.avialableVideoPixelFormatTypes())
output.videoSettings = [
kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32BGRA)
] as String: Any
https://developer.apple.com/documentation/avfoundation/avcapturevideodataoutput/1389945-videosettings
YCbCr 形式で受け取りつつシェーダで色空間を変換するという方法も取れる。
Metal で AR の描画をしたかったので調べた - ピクセルフォーマット編 -
手軽さでは videoSettings で設定する方が良いけれど、パフォーマンスは特に計測していないので、シェーダを利用した変換の方が優れているということはありそう。
https://kentaroid.com/kcvpixelformattypeについての考察/
CVPixelBuffer
CVPixelBuffer の初期化時も同様のキー値で指定できる。
code:swift
var pixelBufferPool: CVPixelBufferPool? = nil
let attributes: NSString: NSObject = [
kCVPixelBufferPixelFormatTypeKey: NSNumber(value: kCVPixelFormatType_32BGRA),
]
CVPixelBufferPoolCreate(nil, nil, attributes as CFDictionary?, &pixelBufferPool)
MTLPixelFormat
Metal を利用する場合はまた別の表現がされている。
MTLPixelFormat - Metal | Apple Developer Documentation
https://qiita.com/edo_m18/items/6e44308d1b865e614f7c#ピクセルフォーマット
参考
https://programmersought.com/article/2088809791/